package com.bdmc.smokesensor.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.bdmc.smokesensor.Utils.Msg;
import com.bdmc.smokesensor.Utils.ResultUtil;
import com.bdmc.smokesensor.entity.Rbac_Function;
import com.bdmc.smokesensor.entity.Sys_Log;
import com.bdmc.smokesensor.models.FunctionTableTree;
import com.bdmc.smokesensor.services.Rbac_FunctionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/function", method = RequestMethod.POST)
public class Rbac_FunctionController extends BaseController{

    @Autowired
    private Rbac_FunctionService _funSvs;


    @RequestMapping(value = "/list")
    public Msg list(@RequestBody Map<String,Object> reqMap) {
        //int page = Integer.valueOf(reqMap.get("page").toString());
        return ResultUtil.success( _funSvs.findAll());
    }

    /**
     * 数据新增或更新，save方法可以执行添加也可以执行更新，如果需要执行持久化的实体存在主键值则更新数据，如果不存在则添加数据。
     */
    @RequestMapping(value = "/save")
    public Msg save(@RequestBody Rbac_Function fun) {
        String action = fun.getId()>0?"修改页面:":"新增页面:";
        Rbac_Function f = _funSvs.save(fun);
        if(f!=null&&f.getId()>0){
            Sys_Log log = new Sys_Log(getLoginName(), getLocalIp() , "功能管理", action+f.getFunName());
            _logSvs.save(log);
            return ResultUtil.success();
        }else
        {
            return ResultUtil.error(-1,"操作失败，请联系管理员。");
        }
        
    }

     /**
     * 删除方法
     */
    @RequestMapping(value = "/delete")
    public Msg delete(@RequestBody Map<String,Integer> reqMap) {
        int id=reqMap.get("id");
        _funSvs.deleteById(id);
        Sys_Log log = new Sys_Log(getLoginName(), getLocalIp() , "功能管理", "删除页面："+id);
            _logSvs.save(log);
        return ResultUtil.success();
    }

     /**
     * 获取所有父级function
     */
    @RequestMapping(value = "/p_function")
    public Msg getParentFunction(@RequestBody Map<String, String> map) {
         int id= Integer.parseInt(map.get("id"));
        return ResultUtil.success(_funSvs.GetParentFunction(id));
    }

    @RequestMapping(value = "/all_list")
    public Msg getAllList(@RequestBody Map<String,Object> map) {
        List<Rbac_Function> list = _funSvs.findAll();
        List<Rbac_Function> childList = list.stream().filter(x->x.getParentFunId()!=0).collect(Collectors.toList());
        List<Rbac_Function> parentList = list.stream().filter(x->x.getParentFunId()==0).collect(Collectors.toList());
        List<FunctionTableTree> funTree= new ArrayList<FunctionTableTree>();
        for(Rbac_Function f:parentList){
            FunctionTableTree tree = new FunctionTableTree();
            tree.id = f.getId();
            tree.funName=f.getFunName();
            tree.iconCss=f.getIconCss();
            tree.funOrder=f.getFunOrder();
            tree.pageUrl=f.getPageUrl();
            tree.parentFunId=f.getParentFunId();
            tree.children=new ArrayList<FunctionTableTree>();
            funTree.add(tree);
        }  
        for(Rbac_Function f:childList){
            FunctionTableTree parent = funTree.stream().filter(x->x.id==f.getParentFunId()).findFirst().orElse(null);
            FunctionTableTree tree = new FunctionTableTree();
            tree.id = f.getId();
            tree.funName=f.getFunName();
            tree.iconCss=f.getIconCss();
            tree.funOrder=f.getFunOrder();
            tree.pageUrl=f.getPageUrl();
            tree.parentFunId=f.getParentFunId();
            parent.children.add(tree);
        }
        return ResultUtil.success(funTree);
    }




}
